﻿using Acme.BookStore.Books;
using Acme.BookStore.Bookss;
using Acme.BookStore.Orderinforss;
using BWShop.Common;
using Microsoft.AspNetCore.Mvc;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;

namespace Acme.BookStore
{
    public class OrderinforAppService : ApplicationService, IOrderinforAppService
    {
        private RedisHelper redisHelper = new RedisHelper();
        private readonly IRepository<Orderinfor, int> _orderinfors;
        private readonly IRepository<OrderShop, int> _orderShops;
        private readonly IRepository<Goods, int> _goods;
        private readonly IRepository<FileImage, int> _fileImages;
        public OrderinforAppService(IRepository<Orderinfor, int> orderinfors, IRepository<OrderShop, int> orderShops, IRepository<Goods, int> goods, IRepository<FileImage, int> fileImages)
        {
            _orderinfors = orderinfors;
            _orderShops = orderShops;
            _goods = goods;
            _fileImages = fileImages;
        }

        //添加订单
        [HttpPost]
        public int OrderCreate(CreateUpdateOrderinforDto dto)
        {
            // List<CreateUpdateOrderShopDto> shopDtos = dto.CreateUpdateOrderShopDtos;
            //雪花id
            var idwork = new IdWorker(1, 1).NextId().ToString();

            var order = new Orderinfor()
            {
                orderInfoId = idwork,
                payWayInfoId = dto.payWayInfoId,
                // usersId = dto.usersId,
                orderInfoAddre = dto.orderInfoAddre,
                orderInfoName = dto.orderInfoName,
                shoppAddreTel = dto.shoppAddreTel,
                orderInfoSalePrice = dto.orderInfoSalePrice,
                commodCountPrice = dto.commodCountPrice,
                orderInfoBarCode = dto.orderInfoBarCode,
                orderInfoPayPrice = dto.orderInfoPayPrice,
                orderInfoActivePrice = dto.orderInfoActivePrice,
                orderInfoWaybillNum = idwork
            };
            _orderinfors.InsertAsync(order);
            List<OrderShop> orderShops = new List<OrderShop>();
            foreach (var item in dto.CreateUpdateOrderShopDtos)
            {
                orderShops.Add(new OrderShop()
                {
                    orderInfoId = idwork.ToString(),
                    productId = item.productId,
                    productSum = item.productSum,
                    CountPrice = dto.commodCountPrice
                });
            }
            _orderShops.InsertManyAsync(orderShops);
            return 1;
        }
        //订单显示
        [HttpGet]
        public List<OrderShopDto> GetList(OrderListDto input)
        {
            var list = (from a in _orderinfors
                        orderby a.Id descending
                        select new OrderShopDto
                        {
                            Id=a.Id,
                            orderInfoId = a.orderInfoId,
                            orderInfoTime = a.orderInfoTime,
                            orderInfoAddre = a.orderInfoAddre,
                            orderInfoName = a.orderInfoName,
                            shoppAddreTel = a.shoppAddreTel,
                            orderInfoState = a.orderInfoState,
                            orderInfoPayPrice = a.orderInfoPayPrice,
                            orderInfoSalePrice=a.orderInfoSalePrice,
                            orderDetailList = (
                                   from fb in _orderShops
                                   join g in _goods
                                   on fb.productId equals g.Id
                                  //join t in _fileImages
                                   //on g.Id equals t.imageSId
                                   where fb.orderInfoId == a.orderInfoId
                                   select new OrderDetailListDto
                                   {
                                       orderInfoId=fb.orderInfoId,
                                       productId =fb.productId,
                                       productSum = fb.productSum,
                                       CountPrice = fb.CountPrice,
                                       productName = g.productName,
                                      // imageURL = t.IamgeUrl
                                   }).ToList()
                            
                        }).AsSingleQuery<OrderShopDto>().ToList();
            list = list.WhereIf(!string.IsNullOrEmpty(input.orderInfoId), p => p.orderInfoId == input.orderInfoId)
                .WhereIf(input.orderInfoState >= 0, p => p.orderInfoState == input.orderInfoState).ToList();
            var totalCount = list.Count();
            return  list;
        }
    }
}